package com.bc004346.travelus;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bc004346.travelus.model.domain.Passenger;
import com.bc004346.travelus.utility.Utility;

import java.util.List;

import static com.bc004346.travelus.utility.Utility.log;
import static com.bc004346.travelus.utility.Utility.onActivityStart;
import static com.bc004346.travelus.utility.Utility.onActivityStop;

/**
 * Created by Renats on 4/3/2014.
 * Contact details confirmation
 */
public class ContactDetailsActivity extends Activity {

    //<editor-fold desc="Members">

    private EditText mPhone;
    private EditText mEmail;
    private TextView mMessage;
    private List<Passenger> mPassengers;

    //</editor-fold>
    //<editor-fold desc="Properties">

    public String getPhone() {
        Editable x = mPhone.getText();
        return x == null ? "" : x.toString();
    }

    public String getEmail() {
        Editable x = mEmail.getText();
        return x == null ? "" : x.toString();
    }

    //</editor-fold>
    //<editor-fold desc="Overrides">

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_details);

        setupUI();
    }

    @Override
    protected void onStart() {
        super.onStart();
        onActivityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        onActivityStop(this);
    }

    //</editor-fold>
    //<editor-fold desc="Private Methods">

    private void setupUI() {
        try {
            mPhone = (EditText) findViewById(R.id.activity_contact_details_editText_phone);
            mEmail = (EditText) findViewById(R.id.activity_contact_details_editText_email);
            mMessage = (TextView) findViewById(R.id.activity_contact_details_textView_message);
            Button mButton = (Button) findViewById(R.id.activity_contact_details_button_continue);

            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isInputValid()) selectPassengers();
                }
            });

            mPassengers = Utility.paxSelection;
            if (mPassengers.size() > 0) {
                String phone = mPassengers.get(0).getPhone();
                String email = mPassengers.get(0).getEmail();
                mPhone.setText(phone == null ? "" : phone);
                mEmail.setText(email == null ? "" : email);
            }
        } catch (Exception e) {
            mMessage.setText(e.getMessage());
            log(e, this);
        }
    }

    private void selectPassengers() {

        try {
            for (Passenger pax : mPassengers) {
                pax.setPhone(getPhone());
                pax.setEmail(getEmail());
            }
            Utility.paxSelection = mPassengers;

            Intent intent = new Intent(this, PassengerSelectionActivity.class);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            mMessage.setText(e.getMessage());
            log(e, this);
        }
    }

    private boolean isInputValid() {
        try {
            StringBuilder message = new StringBuilder();
            if (getPhone().length() != 10)
                message.append("phone number must be 10 char long\n\r");
            if (getEmail().length() < 5)
                message.append("email is invalid");
            if (message.length() == 0)
                return true;

            mMessage.setText(message.toString());
        } catch (Exception e) {
            mMessage.setText(e.getMessage());
            log(e, this);
        }
        return false;
    }

    //</editor-fold>
}
